package org.hackystat.sdsa.analyzer.control.processor;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.logging.Logger;

import javax.xml.datatype.XMLGregorianCalendar;

import org.hackystat.sdsa.analyzer.model.activity.Activity;
import org.hackystat.sdsa.analyzer.model.activity.CompilationActivity;
import org.hackystat.sensorbase.resource.sensordata.jaxb.Properties;
import org.hackystat.sensorbase.resource.sensordata.jaxb.Property;
import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorData;
import org.hackystat.utilities.tstamp.Tstamp;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

/**
 * Test CompileActivityProcessor.
 * 
 * @author hongbing
 *
 */
public class TestCompilationActivityProcessor {
  /** Compile activity processor */
  private CompilationActivityProcessor processor;
  private Logger logger;
  
  /**
   * Prepares test variables.
   * 
   * @throws Exception If error in testing. 
   */
  @Before public void setUp() throws Exception {
    this.processor = new CompilationActivityProcessor();
    this.logger = Logger.getLogger(TestCompilationActivityProcessor.class.getName());
  }
  
  /**
   * Test processing of compilation activity.
   * 
   * @throws Exception If any error happens during testing.
   */
  @Test public void testCompilation() throws Exception {
    /** Compilation data. */
    SensorData compilationData = new SensorData();
    XMLGregorianCalendar tstamp = Tstamp.makeTimestamp("2007-11-17T10:25:16.949-08:00");
    compilationData.setTimestamp(tstamp);
    compilationData.setResource("file:/C:/cvs/work/com/foo/Bar.java");
    compilationData.setSensorDataType("DevEvent");
    
    // Add property
    Properties props = new Properties();
    compilationData.setProperties(props);

    Property type = new Property();
    type.setKey("Type");
    type.setValue("Build");
    props.getProperty().add(type);
    
    Property success = new Property();
    success.setKey(CompilationActivity.SUCCESS);
    success.setValue("false");
    props.getProperty().add(success);

    Property error = new Property();
    error.setKey(CompilationActivity.ERROR);
    error.setValue("foo is not defined.");
    props.getProperty().add(error);

    EditActivityProcessor editProcessor = new EditActivityProcessor();
    assertFalse("Test processable of document edit activity", 
        editProcessor.processable(compilationData));

    assertTrue("Test processable of compilation activity", 
           this.processor.processable(compilationData));
    
    Activity activity = this.processor.process(compilationData, logger);
    Assert.assertTrue("Test return type", activity instanceof CompilationActivity);

    
    CompilationActivity compilationActivity = (CompilationActivity) activity;
    assertFalse("Test successfulness", compilationActivity.isSuccessful());
    assertEquals("Test compilation error", "foo is not defined.", 
        compilationActivity.getErrorMessage());
  }

}
